﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpDrawEllipseArcModelLineBehavior.h"
#include "IUiView.h"
#include "IModelView.h"
#include "IDocument.h"
#include "IUserTransaction.h"
#include "IModelLine.h"
#include "IEllipse3d.h"
#include "IElementBasicInformation.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

GbmpDrawEllipseArcModelLineBehavior::~GbmpDrawEllipseArcModelLineBehavior()
{
}

bool GbmpDrawEllipseArcModelLineBehavior::OnInitAction(IUiView* pCurrentView)
{
    return true;
}

ElementId GbmpDrawEllipseArcModelLineBehavior::OnDrawEllipseArcFinished(IUiView* pCurrentView, OwnerPtr<IEllipse3d> opGeomEllipseArc)
{
    DBG_WARN_AND_RETURN_UNLESS(pCurrentView, ElementId::InvalidID, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_UNLESS(opGeomEllipseArc, ElementId::InvalidID, L"opGeomEllipseArc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = pCurrentView->GetModelView();
    DBG_WARN_AND_RETURN_UNLESS(pModelView, ElementId::InvalidID, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = pModelView->GetDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc, ElementId::InvalidID, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"绘制椭圆弧模型线"));
    DBG_WARN_AND_RETURN_UNLESS(opTransaction.get(), ElementId::InvalidID, L"opTransaction为空",L"GDMPLab",L"2024-03-30");

    ElementId planeId = pModelView->GetWorkPlaneId();
    DBG_WARN_AND_RETURN_UNLESS(planeId.IsValid(), ElementId::InvalidID, L"planeId无效",L"GDMPLab",L"2024-03-30");

    IModelLine* pModelLine = nullptr;
    //if (planeId.IsValid())
    //{
    //    Coordinate3d planeCoord = GbmpUiPlatformUtils::GetPlaneCoordinate3d(pDoc, planeId);
    //    OwnerPtr<IPlane> opPlane = IPlane::Create(planeCoord.GetOrigin(), planeCoord.GetX(), planeCoord.GetY());
    //    OwnerPtr<ICurve2d> opCurve2d = AlgorithmProject::Project(opGeomEllipseArc.get(), opPlane.get());
    //    pModelLine = IModelLine::Create(pDoc, planeId, TransferOwnership(opCurve2d), ElementCreationOptions::Normal);
    //}
    //else
    {
        pModelLine = IModelLine::Create(pDoc, TransferOwnership(opGeomEllipseArc));
    }
    DBG_WARN_AND_RETURN_UNLESS(pModelLine, ElementId::InvalidID, L"创建ModelLine失败",L"GDMPLab",L"2024-03-30");

    opTransaction->Commit();

    IElement* pElement = pModelLine->GetOwnerElement();
    DBG_WARN_AND_RETURN_UNLESS(pElement, ElementId::InvalidID, L"创建ModelLine失败",L"GDMPLab",L"2024-03-30");

    IElementBasicInformation * pElemBasicInfo = pElement->GetBasicInformation();
    DBG_WARN_AND_RETURN_UNLESS(pElemBasicInfo, ElementId::InvalidID, L"创建ModelLine失败",L"GDMPLab",L"2024-03-30");

    ElementId modelLineId = pElemBasicInfo->GetElementId();
    DBG_WARN_AND_RETURN_UNLESS(modelLineId.IsValid(), ElementId::InvalidID, L"modelLineId无效",L"GDMPLab",L"2024-03-30");

    return modelLineId;
}

void GbmpDrawEllipseArcModelLineBehavior::OnDestroyAction()
{
}
